﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DreamOnGameEngine.Utils
{
    /// <summary>
    /// Parses commandline
    /// </summary>
    public class CommandLine
    {
        private Dictionary<string,string> Parameters;
        /// <summiary>
        /// Constructs a commandline from string
        /// </summary>
        /// <param name="CommandLine">Commandline to be parsed</param>
        public CommandLine(string[] CommandLine)
        {
            Parameters = new Dictionary<string, string>();
            Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

            Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);
            string Parameter = null;
            string[] Parts;

            // Valid parameters forms
            // {-,/,--}param{ ,=,:}((",')value
            // Examples
            // -param1 value1 --param2 /param3:"Test-:-work
            //   /param4=happy -param5 '--=nice=--'
            foreach (string Txt in CommandLine)
            {
                Parts = Spliter.Split(Txt, 3);
                switch (Parts.Length)
                {
                    case 1:
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                            {
                                Parts[0] = Remover.Replace(Parts[0], "$1");
                                Parameters.Add(Parameter, Parts[0]);
                            }
                            Parameter = null;
                        }
                        else
                        {
                            throw new Exception();
                        }
                        break;
                    case 2:
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                                Parameters.Add(Parameter, "true");
                        }
                        Parameter = Parts[1];
                        break;
                    case 3:
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                                Parameters.Add(Parameter, "true");
                        }

                        Parameter = Parts[1];
                        if (!Parameters.ContainsKey(Parameter))
                        {
                            Parts[2] = Remover.Replace(Parts[2], "$1");
                            Parameters.Add(Parameter, Parts[2]);
                        }

                        Parameter = null;
                        break;
                }
            }
            if (Parameter != null)
            {
                if (!Parameters.ContainsKey(Parameter))
                    Parameters.Add(Parameter, "true");
            }
        }
        /// <summary>
        /// Shows a param
        /// </summary>
        /// <param name="Param">Param name to get</param>
        /// <returns>Parameter</returns>
        public string this[string Param]
        {
            get
            {
                string Value;
                bool succ = Parameters.TryGetValue(Param, out Value);
                if (!succ)
                    Value = null;
                return (Value);
            }
        }
    }
}
